9. Ridgeline plot

9.1. 概要

9.2. Plotlyによる作図方法

9.3. MADB Labを用いた作図例

9.3.1. 下準備

import pandas as pd
import plotly.express as px

import warnings
warnings.filterwarnings('ignore')
# 前処理の結果,以下に分析対象ファイルが格納されていることを想定
PATH_DATA = '../../data/preprocess/out/magazines.csv'
# Jupyter Book用のPlotlyのrenderer
RENDERER = 'plotly_mimetype+notebook'
def show_fig(fig):
    """Jupyter Bookでも表示可能なようRendererを指定"""
    fig.show(renderer=RENDERER)
def add_years_to_df(df, unit_years=10):
    """unit_years単位で区切ったyears列を追加"""
    df_new = df.copy()
    df_new['years'] = \
        pd.to_datetime(df['datePublished']).dt.year \
        // unit_years * unit_years
    df_new['years'] = df_new['years'].astype(str)
    return df_new
df = pd.read_csv(PATH_DATA)

9.3.2. 年代別・作品別の合計連載週数

df = add_years_to_df(df, unit_years=10)
df_plot = \
    df.value_counts(['years', 'cname']).reset_index(name='weeks')
# 年代順にソート
df_plot = df_plot.sort_values(
    'years', ascending=False, ignore_index=True)
fig = px.violin(
    df_plot, x='weeks', y='years', orientation='h')
fig.update_traces(
    side='positive', scalemode='count', width=4)
show_fig(fig)
fig = px.violin(
    df_plot, x='weeks', y='years', orientation='h')
fig.update_traces(
    side='positive', scalemode='count', width=4)
fig.update_xaxes(range=[0, 200])
show_fig(fig)

9.3.3. 年代別・作者別の合計連載週数

df = add_years_to_df(df, unit_years=10)
df_plot = \
    df.value_counts(['years', 'creator']).reset_index(name='weeks')
# 年代順にソート
df_plot = df_plot.sort_values(
    'years', ascending=False, ignore_index=True)
fig = px.violin(
    df_plot, x='weeks', y='years', orientation='h')
fig.update_traces(
    side='positive', scalemode='count', width=4)
show_fig(fig)
fig = px.violin(
    df_plot, x='weeks', y='years', orientation='h')
fig.update_traces(
    side='positive', scalemode='count', width=4)
fig.update_xaxes(range=[0, 200])
show_fig(fig)